package com.huiquan.slang.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.EnhancedBaseDao;
import com.huiquan.slang.domain.SlangSentence;
import com.huiquan.slang.domain.SlangVocabulary;
import com.ibatis.sqlmap.client.SqlMapClient;

@Repository
public class SlangSentenceDao extends EnhancedBaseDao<SlangSentence> {

	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	public static final String space = "SLANG_SENTENCE.";
	public static final String vocabularySpace = "SLANG_VOCABULARY.";

	public SlangSentenceDao() {
		super(space);
	}

	@SuppressWarnings("unchecked")
	public List<SlangSentence> retrieveListBySids(String sids, Long uid) {
		Map<String, Object> param = new HashMap<>();
		param.put("sids", sids);
		param.put("uid", uid);
		return (List<SlangSentence>) this.list(space + "RETRIEVE_LIST_BY_SIDS", param, sqlMapClient);
	}

	public void labelSentence(SlangSentence sentence, List<SlangVocabulary> newVocabularys, User user)
			throws Exception {
		long start = System.currentTimeMillis();

		// 开启事务
		sqlMapClient.startTransaction();

		// 拼凑句子中的vocabularys
		StringBuffer vocabularys = new StringBuffer();
		vocabularys.append((char) 1);

		// 删除历史vocabulary，将新的分词保存
		Map<String, Object> param = new HashMap<>();
		param.put("sid", sentence.getId());
		delete(vocabularySpace + "DELETE", param, sqlMapClient);

		boolean adFlag = true; // 判断该句子的分词是否都被标注为AD
		for (SlangVocabulary sv : newVocabularys) {
			vocabularys.append(sv.getVocabulary());
			vocabularys.append((char) 1);
			insert(vocabularySpace + "INSERT", sv, sqlMapClient);

			if (!"AD".equals(sv.getProperty())) {
				adFlag = false;
			}
		}

		sentence.setVocabularys(vocabularys.toString());
		if (!SlangSentence.STATUS_LABELED.equals(sentence.getLabelStatus())) {
			sentence.setLabelStatus(SlangSentence.STATUS_LABELED);
			sentence.setLabelUid(user.getUserId());
			sentence.setLabelUname(user.getRealName());
		}
		sentence.setModifierId(user.getUserId());
		sentence.setModifierName(user.getRealName());
		update(space + "UPDATE", sentence, sqlMapClient);

		// 如果是一个分词都是AD的句子，则维护“已标注AD句子ID”表
		if (adFlag) {
			insert(space + "INSERT_LABELED_AD", param, sqlMapClient);
		} else {
			delete(space + "DELETE_LABELED_AD", param, sqlMapClient);
		}

		// 提交关闭事务
		sqlMapClient.executeBatch();
		sqlMapClient.endTransaction();

		logger.info("【Notice】 SlangSentence update sentence service cost [{}]ms.", System.currentTimeMillis() - start);
	}

	public int retrieveUnlabeledSizeByIds(String sids) {
		Map<String, Object> param = new HashMap<>();
		param.put("sids", sids);

		Integer size = (Integer) this.object(space + "RETRIEVE_UNLABELED_SIZE_BY_IDS", param, sqlMapClient);
		return size == null ? 0 : size;
	}

	public void restore(SlangSentence sentence) {
		update(space + "RESTORE", sentence, sqlMapClient);
	}
}
